package Function;

import VWorldGameStudio.FCMonster;
import lombok.Getter;
import lombok.Setter;

import java.util.*;

import static FCRendering.FCRMaster.fcv;

public class FCMonsterBuildCoor {
    //最高到820高度
    private int[] initY5 = new int[]{(int) (90 * fcv.getProportion()),
            (int) (246 * fcv.getProportion()),
            (int) (402 * fcv.getProportion()),
            (int) (558 * fcv.getProportion()),
            (int) (714 * fcv.getProportion())
    };//按计划1列5个的每一个的y坐标 106一个标准格+50 最大可156， 大于用initY4

    private int[] initY4 = new int[]{(int) (90 * fcv.getProportion()),
            (int) (285 * fcv.getProportion()),
            (int) (480 * fcv.getProportion()),
            (int) (675 * fcv.getProportion())
    };//按计划1列4个的每一个的y坐标 145+ 50一个标准格最大是195 大于用initY3

    private int[] initY3 = new int[]{(int) (90 * fcv.getProportion()),
            (int) (350 * fcv.getProportion()),
            (int) (610 * fcv.getProportion())
    };//按计划1列3个的每一个的y坐标  210+50一个标准格最大是260 大于 用initY2

    private int[] initY2 = new int[]{(int) (90 * fcv.getProportion()),
            (int) (480 * fcv.getProportion())
    };//按计划1列2个的每一个的y坐标  340一个标准格最大是390 大于 用initY1
    private int[] initY1 = new int[]{(int) (150 * fcv.getProportion())
    };//按计划1列1个的每一个的y坐标

    private boolean firstAddMonster = true;
    @Getter
    private int leftPadding = 0;//留作当场战斗的记录
    @Getter
    private int maxMonsterW = 0;

    @Getter
    @Setter
    private Integer[] lastCoor = null;//时刻记录最后一个


    //TODO 这个类也必须同步好队长的参数
    public void syncServiceBuildCoor(int[] rLastCoor, int rMaxMonsterW, int rLeftPadding){
        leftPadding = rLeftPadding;
        lastCoor = new Integer[]{rLastCoor[0], rLastCoor[1],rLastCoor[2],rLastCoor[3],rLastCoor[4],rLastCoor[5],rLastCoor[6]};
        firstAddMonster = true;
        maxMonsterW = rMaxMonsterW;
    }

    public void buildMonsterCoor(int monsternum) {
        //表示有3列，一列放最多5个怪或者超出了高度
        leftPadding = (int) (100 * fcv.getProportion());
        maxMonsterW = 0;
        firstAddMonster = true;
        lastCoor = null;
        int monsterIndex = 0;
        for (int r = 0; r < 8; r++) {
            if (monsterIndex == monsternum) {
                break;
            }
            int Jsize = 5;//默认
            int[] initTmpY = initY5;
            int startIndex = monsterIndex;
            for (int i = 0; i < 5; i++) {
                if (startIndex == monsternum) {
                    break;
                }
                int[] m = fcv.getBattleMonster().get(startIndex).getPngSize();
                int mH = (m[1] * 2);

                //  System.out.println("checkmH "+mH);
                if (mH >= 390) {
                    if (initTmpY.length < 1) {
                        break;
                    }
                    //只能放前面1个，所以
                    if (i < 1) {
                        initTmpY = initY1;
                    } else {
                        break;
                    }
                } else if (mH >= 260) {
                    if (initTmpY.length < 2) {
                        break;
                    }
                    //只能放前面2个，所以
                    if (i < 2) {
                        initTmpY = initY2;
                    } else {
                        break;
                    }
                } else if (mH >= 195) {
                    if (initTmpY.length < 3) {
                        break;
                    }
                    //只能放前面3个，所以
                    if (i < 3) {
                        initTmpY = initY3;
                    } else {
                        break;
                    }
                } else if (mH >= 156) {
                    if (initTmpY.length < 4) {
                        //当前的放不下4个，那就要抛弃这个了，直接退出
                        break;
                    }
                    //只能放前面4个，所以
                    if (i < 4) {
                        initTmpY = initY4;
                    } else {
                        break;
                    }
                } else {//因为默认已经是initTmpY = initY5了；

                }
                startIndex++;
            }
            Jsize = initTmpY.length;
            //    System.out.println("jZIse "+Jsize);


            for (int j = 0; j < initTmpY.length; j++) {
                if (monsterIndex == monsternum) {
                    return;
                }
                int yindex = 0;
                if (Jsize == 5) {
                    switch (j) {
                        case 0:
                            yindex = 2;
                            break;
                        case 1:
                            yindex = 1;
                            break;
                        case 2:
                            yindex = 3;
                            break;
                        case 3:
                            yindex = 0;
                            break;
                        case 4:
                            yindex = 4;
                            break;
                    }
                } else if (Jsize == 4) {
                    switch (j) {
                        case 0:
                            yindex = 1;
                            break;
                        case 1:
                            yindex = 2;
                            break;
                        case 2:
                            yindex = 0;
                            break;
                        case 3:
                            yindex = 3;
                            break;
                    }
                } else if (Jsize == 3) {
                    switch (j) {
                        case 0:
                            yindex = 2;
                            break;
                        case 1:
                            yindex = 0;
                            break;
                        case 2:
                            yindex = 1;
                            break;
                    }
                } else if (Jsize == 2) {
                    switch (j) {
                        case 0:
                            yindex = 0;
                            break;
                        case 1:
                            yindex = 1;
                            break;
                    }
                }
                //  System.out.println("monsterIndex "+monsterIndex+" Jsize:"+Jsize+" yindex:"+yindex+" initTmpY[yindex]="+initTmpY[yindex]);
                int[] chicun = fcv.getBattleMonster().get(monsterIndex).getPngSize();
                int fmW = (int) (chicun[0] * 2 * fcv.getProportion());
                int fmH = (int) (chicun[1] * 2 * fcv.getProportion());


                if ((leftPadding + fmW) > (int) (1200 * fcv.getProportion())) {
                    //TODO 放不下了
                    int delNums = fcv.getBattleMonster().size() - fcv.getBattleMonsterCoor().size();
                    for (int d = 0; d < delNums; d++) {
                        fcv.getBattleMonster().remove(fcv.getBattleMonster().size() - 1);
                    }
                    System.out.println("fcv.getBattleMonsterCoor().size() " + fcv.getBattleMonsterCoor().size() + " " + fcv.getBattleMonster().size());
                    return;
                }
                if (maxMonsterW == 0) {
                    maxMonsterW = fmW;
                } else {
                    maxMonsterW = Math.max(maxMonsterW, fmW);
                }
                lastCoor = new Integer[]{leftPadding,initTmpY[yindex],fmW, fmH, r, Jsize, j};
                fcv.getBattleMonsterCoor().add(lastCoor);// 写入怪物坐标和尺寸,第r+1列的Jsize规格的第j行
                monsterIndex++;
            }
            if (monsterIndex == monsternum) {
                return;
            }
            leftPadding = leftPadding + maxMonsterW + (int) (30 * fcv.getProportion());
        }
    }

    public Integer[] canAddNewMonster(FCMonster ms) {
        //STEP 1；//检查已经死了的怪有没位置插
        int[] m = ms.getPngSize();
        int mH = (m[1] * 2);
        int fmW = (int) (m[0] * 2 * fcv.getProportion());
        int fmH = (int) (m[1] * 2 * fcv.getProportion());
        int Jsize = 5;
        //  System.out.println("checkmH "+mH);
        if (mH >= 390) {
            Jsize = 1;
        } else if (mH >= 260) {
            Jsize = 2;
        } else if (mH >= 195) {
            Jsize = 3;
        } else if (mH >= 156) {
            Jsize = 4;
        }

        HashMap<String,Integer[]> find = new HashMap<>();
        for (int i = 0; i < fcv.getBattleMonster().size(); i++) {
            FCMonster monster = fcv.getBattleMonster().get(i);
            int nJs = fcv.getBattleMonsterCoor().get(i)[5];
            if (Jsize == nJs) {
                if (monster.getIsDeath()) {
                    //可以插入这个位置
                    Integer[] ret = fcv.getBattleMonsterCoor().get(i).clone();
                    ret[2] = fmW;
                    ret[3] = fmH;
                    find.put(ret[0]+"*"+ret[1], ret);
                }else {
                    Integer[] ret2 = fcv.getBattleMonsterCoor().get(i);
                    if(find.containsKey(ret2[0]+"*"+ret2[1])){
                        find.remove(ret2[0]+"*"+ret2[1]);
                    }
                }
            }
        }
        if(find.size() > 0){
            Iterator<Map.Entry<String,Integer[]>> mapIte = find.entrySet().iterator();
            return mapIte.next().getValue();
        }

        //STEP 2 只能在后排添加了
        Integer[] last = lastCoor;
        if (last[5] == Jsize) {
            //TODO 判断是否后面还有位置
            int[] initTmpY = initY5;
            int yindex = -1;
            System.out.println("last[6]+1 =" + (last[6] + 1) + " Jsize " + Jsize);
            if (Jsize == 5) {
                initTmpY = initY5;
                switch (last[6] + 1) {
                    case 0:
                        yindex = 2;
                        break;
                    case 1:
                        yindex = 1;
                        break;
                    case 2:
                        yindex = 3;
                        break;
                    case 3:
                        yindex = 0;
                        break;
                    case 4:
                        yindex = 4;
                        break;
                }
            } else if (Jsize == 4) {
                initTmpY = initY4;
                switch (last[6] + 1) {
                    case 0:
                        yindex = 1;
                        break;
                    case 1:
                        yindex = 2;
                        break;
                    case 2:
                        yindex = 0;
                        break;
                    case 3:
                        yindex = 3;
                        break;
                }
            } else if (Jsize == 3) {
                initTmpY = initY3;
                switch (last[6] + 1) {
                    case 0:
                        yindex = 2;
                        break;
                    case 1:
                        yindex = 0;
                        break;
                    case 2:
                        yindex = 1;
                        break;
                }
            } else if (Jsize == 2) {
                initTmpY = initY2;
                switch (last[6] + 1) {
                    case 0:
                        yindex = 0;
                        break;
                    case 1:
                        yindex = 1;
                        break;
                }
            }
            System.out.println("yindex " + yindex+" "+leftPadding);
            if (yindex == -1) {
                //TODO 放不下了，换下一行放了
                if (Jsize == 5) {
                    yindex = 2;
                } else if (Jsize == 4) {
                    yindex = 1;
                } else if (Jsize == 3) {
                    yindex = 2;
                } else if (Jsize == 2) {
                    yindex = 0;
                } else {
                    yindex = 0;
                }
                if ((leftPadding + fmW) > (int) (1200 * fcv.getProportion())) {
                    //TODO 放不下了
                    return null;
                }
                System.out.println("yindex2= " + yindex+" "+leftPadding);
                maxMonsterW = Math.max(maxMonsterW, fmW);
                leftPadding = leftPadding + maxMonsterW + (int) (30 * fcv.getProportion());
                Integer[] ret = new Integer[]{leftPadding, initTmpY[yindex], fmW, fmH, 0, Jsize, 0};// 写入怪物坐标和尺寸,第r+1列的Jsize规格的第j行
                lastCoor = ret;
                return ret;
            } else {
                maxMonsterW = Math.max(maxMonsterW, fmW);
                Integer[] ret = new Integer[]{leftPadding,
                        initTmpY[yindex], fmW, fmH, 0, Jsize, last[6] + 1};
                lastCoor = ret;
                return ret;
            }
        }
        return null;
    }
}
